#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace IO
{
	template<typename T>
	void read(T &_x){_x=0;int _f=1;char ch=getchar();while(!isdigit(ch)) _f=(ch=='-'?-1:_f),ch=getchar();while(isdigit(ch)) _x=_x*10+(ch^48),ch=getchar();_x*=_f;}
	template<typename T,typename... Args>
	void read(T &_x,Args&...others){Read(_x);Read(others...);}
	const int BUF=20000000;char buf[BUF],to,stk[32];int plen;
	#define pc(x) buf[plen++]=x
	#define flush(); fwrite(buf,1,plen,stdout),plen=0;
	template<typename T>inline void print(T x){if(!x){pc(48);return;}if(x<0) x=-x,pc('-');for(;x;x/=10) stk[++to]=48+x%10;while(to) pc(stk[to--]);}
}
using namespace IO;
const int N = 1e5+10;
int n,mi,cmi,mi1[N],cmi1[N],a[N],ans;
struct w
{
	int a,b;
}c[N];
inline bool cmp(w x,w y){return x.b<y.b;}
signed main()
{
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	read(n);
	for(int i = 1;i <= n;i++) read(c[i].a),read(c[i].b);
	sort(c+1,c+1+n,cmp);
	mi1[n+1] = cmi1[n+1] = 1e15;
	for(int i = n;i >= 1;i--) 
	{
		mi1[i] = mi1[i+1],cmi1[i] = cmi1[i+1];
		if(c[i].a+c[i].b<mi1[i]) cmi1[i] = mi1[i],mi1[i] = c[i].a+c[i].b;
		else if(c[i].a+c[i].b<cmi1[i]) cmi1[i] = c[i].a+c[i].b;
	}
	mi = cmi = ans = 1e15;
	for(int i = 1;i <= n;i++)
	{
		a[1] = mi+c[i].b,a[2] = cmi+c[i].b,a[3] = mi1[i+1],a[4] = cmi1[i+1];
		sort(a+1,a+5); ans = min(ans,a[1]+a[2]+c[i].a); 
		if(c[i].a<mi) cmi = mi,mi = c[i].a;
		else if(c[i].a<cmi) cmi = c[i].a;
	}
	print(ans); flush();
	return 0;
}
